-- ================================START====================================
-- Template generated from		FRAMEWORKIVY CODE GENERATOR
-- Generator Version:			<1.0.0.0>
-- Template Creation Time:		<Thursday, 26 May, 2016   12:32:16 AM>
-- Template Modification Time:	<Monday, 23 May, 2016   11:22:57 PM>
-- Code Generation Time:		<Thursday, 26 May, 2016   12:52:16 AM>
-- Workstation ID:				<PRAVESH>
-- Current system User ID:		<pravesh kr>
-- Entity/Table Name:			<User>
-- Description:					<SQL Server Stored Procedure For Insert>
-- Template File:				<IvyGodeGenDefaultTemplate V1.1.xml>

-- Please modify the generated code as per requirement.
-- This code generator is just to speed up the stereo type codes
-- Complex logic needs to be modified manually.

-- This block of comments will not be included in
-- the definition of the procedure.
-- =========================================================================

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Ivy_UserManagement_User_Insert')
	BEGIN
		DROP  Procedure  [Ivy_UserManagement_User_Insert]
	END
GO

CREATE PROCEDURE [Ivy_UserManagement_User_Insert]
-- =========================================================================
-- Created by				FRAMEWORKIVY CODE GENERATOR
-- Generator Version:		<1.0.0.0>
-- Template version used:   <1.0>
-- Template created by:		<Mrinmoy Das>
-- Code generated by:		<pravesh kr>
-- Code generation Time:	<Thursday, 26 May, 2016   12:52:16 AM>
-- Entity/Table Name:		<User>
-- Description:             <SQL Server Stored Procedure For Insert>
-- =========================================================================

-- Add the parameters for the stored procedure here

	(
				@UserPersonKey int = NULL, 
				@UserLoginId varchar(50) = NULL, 
				@UserPassword varchar(50) = NULL, 
				@UserLoginIdType int = NULL, 
				@UserForgotPasswordQuestion varchar(300) = NULL, 
				@UserForgotPasswordAnswer varchar(300) = NULL, 

				@Sys_CreatedByID varchar(50) = NULL, 
				@Sys_ModifiedByID varchar(50) = NULL, 
				@Sys_DeleteType int = NULL, 
				@Sys_IsDeleted bit = 0, 
				@Sys_OrganizationID int = NULL, 
				@Sys_InitialPrimaryKeys varchar(MAX) = NULL, 
				@Sys_WorkstationName varchar(50) = NULL, 


				@Sys_DateOfCreation datetime OUTPUT 
	)
AS
BEGIN
	-- SET NOCOUNT OFF added to indicate how many records are inserted 
	SET NOCOUNT OFF;

	IF @Sys_WorkstationName = 'Default'	
	BEGIN
		SET @Sys_WorkstationName = (SELECT @@SERVERNAME);
	END

	SET @Sys_DateOfCreation = GETDATE();

    INSERT INTO [User]
           (
				UserPersonKey,
				UserLoginId,
				UserPassword,
				UserLoginIdType,
				UserForgotPasswordQuestion,
				UserForgotPasswordAnswer,

				Sys_DateOfCreation,
				Sys_DateOfModification,
				Sys_CreatedByID,
				Sys_ModifiedByID,
				Sys_DeleteType,
				Sys_IsDeleted,
				Sys_OrganizationID,
				Sys_InitialPrimaryKeys,
				Sys_WorkstationName
           )
     VALUES
           (
				@UserPersonKey, 
				@UserLoginId, 
				@UserPassword, 
				@UserLoginIdType, 
				@UserForgotPasswordQuestion, 
				@UserForgotPasswordAnswer, 

				@Sys_DateOfCreation, 
				@Sys_DateOfCreation, 
				@Sys_CreatedByID, 
				@Sys_CreatedByID, 
				@Sys_DeleteType, 
				@Sys_IsDeleted, 
				@Sys_OrganizationID, 
				@Sys_InitialPrimaryKeys, 
				@Sys_WorkstationName 
           );

	--CREATE THE DEFAULT ROLE
	
	DECLARE @UserRoleKey int;
	DECLARE @CreatorID varchar(50);
	DECLARE @OrganizationCode varchar(50);
	DECLARE @OrganizationName varchar(200);
	DECLARE @RoleName varchar(50)

	SET @UserRoleKey =(SELECT [RoleKey] FROM [Sys_RoleMaster] WHERE  [RoleCode] = 'SYSREGUSR');
	SET @CreatorID = (SELECT CONVERT(varchar(10),@UserPersonKey));
	SET @OrganizationCode = (SELECT [OrganizationCode] FROM [Sys_OrganizationMaster] WHERE [OrganizationID] = -1);
	SET @OrganizationName = (SELECT [OrganizationName] FROM [Sys_OrganizationMaster] WHERE [OrganizationID] = -1);
	SET @RoleName = (SELECT [RoleName] FROM [Sys_RoleMaster] WHERE [RoleKey] = @UserRoleKey);

	INSERT [UserOrganizationRole] ([UserKey], [UserOrganizationKey], [UserRoleKey], [UserLoginID], [UserOrganizationCode], [UserOrganizationName], [UserRoleCode], [UserRoleName], [Sys_DateOfCreation], [Sys_DateOfModification], [Sys_CreatedByID], [Sys_ModifiedByID], [Sys_DeleteType], [Sys_IsDeleted], [Sys_OrganizationID], [Sys_InitialPrimaryKeys], [Sys_WorkstationName]) VALUES (@UserPersonKey, -1, @UserRoleKey, @UserLoginId, @OrganizationCode, @OrganizationName, N'SYSREGUSR', @RoleName, GETDATE(), GETDATE(), @CreatorID, @CreatorID, 1, 0, @Sys_OrganizationID, NULL, (SELECT @@SERVERNAME))
	

END
GO

-- =================================END=====================================

